package com.specialized.coupon.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.specialized.common.core.domain.Result;
import com.specialized.coupon.domain.CouponSession;
import com.specialized.coupon.domain.InviteDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
//import com.specialized.common.log.annotation.Log;
//import com.specialized.common.log.enums.BusinessType;
import com.specialized.coupon.domain.Coupon;
import com.specialized.coupon.service.ICouponService;
import com.specialized.common.core.web.controller.BaseController;
import com.specialized.common.core.utils.poi.ExcelUtil;

/**
 * 优惠卷用户关系Controller
 *
 * @author djh
 * @date 2024-07-19
 */
@RestController
@RequestMapping("/coupon")
public class CouponController extends BaseController {


    @Autowired
    private ICouponService couponService;

/**
 * 查询优惠卷用户关系列表
 */
/*@RequiresPermissions("coupon:coupon:list")*/
@GetMapping("/list")
    public Result list(Coupon coupon) {
        startPage();
        List<Coupon> list = couponService.selectCouponList(coupon);
        return getDataTable(list);
    }

    /**
     * 导出优惠卷用户关系列表
     */
/*    @RequiresPermissions("coupon:coupon:export")*/
//    @Log(title = "优惠卷用户关系", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Coupon coupon) {
        List<Coupon> list = couponService.selectCouponList(coupon);
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon. class);
        util.exportExcel(response, list, "优惠卷用户关系数据");
    }
    @PostMapping("/getcoupon")
    public Result getCoupon(){
       return couponService.getCoupon();
    }

    /**
     * 获取优惠卷用户关系详细信息
     */
   /* @RequiresPermissions("coupon:coupon:query")*/
    @GetMapping(value = "/{userCouponId}")
    public Result getInfo(@PathVariable("userCouponId") Long userCouponId) {
        return success(couponService.selectCouponByUserCouponId(userCouponId));
    }

    /**
     * 新增优惠卷用户关系
     */
   /* @RequiresPermissions("coupon:coupon:add")*/
//    @Log(title = "优惠卷用户关系", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody Coupon coupon) {
        return toAjax(couponService.insertCoupon(coupon));
    }

    /**
     * 修改优惠卷用户关系
     */
/*    @RequiresPermissions("coupon:coupon:edit")*/
//    @Log(title = "优惠卷用户关系", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody Coupon coupon) {
        return toAjax(couponService.updateCoupon(coupon));
    }

    /**
     * 删除优惠卷用户关系
     */
 /*   @RequiresPermissions("coupon:coupon:remove")*/
//    @Log(title = "优惠卷用户关系", businessType = BusinessType.DELETE)
    @DeleteMapping("/{userCouponIds}")
    public Result remove(@PathVariable Long[] userCouponIds) {
        return toAjax(couponService.deleteCouponByUserCouponIds(userCouponIds));
    }

    /**
     * 邀请用户生成链接
     */
    @GetMapping("/getUrl")
    public Result getUrl(){
        return couponService.getUrl();
    }
    /**
     * 邀请好友
     */
    @PostMapping("/invite")
    public Result invite(@RequestBody InviteDto dto){
        return couponService.invite(dto);
    }

    /**
     * 抢券
     */
    @PostMapping("/couponSnatching/{sessionId}")
    public Result couponSnatching(@PathVariable("sessionId")Long sessionId){
        return couponService.couponSnatching(sessionId);
    }
    /**
     * 查找场次
     */
    @PostMapping("/findSession")
    public Result sessionList(){
        return couponService.findSession();
    }
    /**
     * 解决幂等
     */
    public Result getRandom(){
        return couponService.getRandom();
    }
    /**
     * 抢券业务
     */
    @GetMapping("/qiang/{sessionId}/{couponId}")
    public Result qiang(@PathVariable("sessionId") Long sessionId,@PathVariable("couponId") Long couponId){

        return couponService.snatching(sessionId,couponId);
    }

}
